/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.restclient.services;

import cz.muni.fi.pa165.machineryrental.dto.MachineDTO;
import cz.muni.fi.pa165.machineryrental.restclient.App;
import cz.muni.fi.pa165.machineryrental.restclient.utils.Utils;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author jostri
 */
public class MachineRestServiceImpl implements MachineRestService {

    private final static String URL_GET_BY_ID = "machineRest/getById/";
    private final static String URL_FIND_ALL = "machineRest/findAll";
    private final static String URL_REMOVE = "machineRest/remove/";
    private final static String URL_CREATE = "machineRest/create";
    private final static String URL_UPDATE = "machineRest/update/";
    private final static String URL_FIND_BY_CODE = "machineRest/findByCode/";
    private final static String URL_FIND_BY_BRAND = "machineRest/findByBrand/";
    private final static String URL_FIND_BY_MODEL = "machineRest/findByModel/";
    private final static String URL_IS_CODE_UNIQUE = "machineRest/isCodeUnique/";

    @Override
    public MachineDTO getById(long id) {

        RestTemplate restTemplate = new RestTemplate();


        String url = App.getUrl() + MachineRestServiceImpl.URL_GET_BY_ID + id;
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);

        ResponseEntity<MachineDTO> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createJSONHeaders(username, password)),
                MachineDTO.class);

        return response.getBody();

    }

    @Override
    public List<LinkedHashMap> findAll() {

        RestTemplate restTemplate = new RestTemplate();


        String url = App.getUrl() + MachineRestServiceImpl.URL_FIND_ALL;
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);


        ResponseEntity<List> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createJSONHeaders(username, password)),
                List.class);

        return response.getBody();
    }

    public void remove(long id) {

        RestTemplate restTemplate = new RestTemplate();


        String url = App.getUrl() + MachineRestServiceImpl.URL_REMOVE + id;
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);


        ResponseEntity<Void> response =
                restTemplate.exchange(url, HttpMethod.DELETE,
                new HttpEntity<Object>(Utils.createJSONHeaders(username, password)),
                Void.class);

    }

    public void create(MachineDTO machine) {

        RestTemplate restTemplate = new RestTemplate();


        String url = App.getUrl() + MachineRestServiceImpl.URL_CREATE;
        String username = App.getUsername();
        String password = App.getPassword();

        ResponseEntity<Void> response =
                restTemplate.exchange(url, HttpMethod.POST,
                new HttpEntity<Object>(machine, Utils.createJSONHeaders(username, password)),
                Void.class);

    }

    public void update(MachineDTO machine) {
        RestTemplate restTemplate = new RestTemplate();


        String url = App.getUrl() + MachineRestServiceImpl.URL_UPDATE + machine.getId();
        String username = App.getUsername();
        String password = App.getPassword();

        ResponseEntity<Void> response =
                restTemplate.exchange(url, HttpMethod.PUT,
                new HttpEntity<Object>(machine, Utils.createJSONHeaders(username, password)),
                Void.class);
    }

    public MachineDTO findByCode(String code) {

        RestTemplate restTemplate = new RestTemplate();


        String url = App.getUrl() + MachineRestServiceImpl.URL_FIND_BY_CODE + code;
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);

        ResponseEntity<MachineDTO> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createJSONHeaders(username, password)),
                MachineDTO.class);

        return response.getBody();


    }

    public List<LinkedHashMap> findByBrand(String brand) {

        RestTemplate restTemplate = new RestTemplate();

        String url = App.getUrl() + MachineRestServiceImpl.URL_FIND_BY_BRAND + brand;
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);


        ResponseEntity<List> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createJSONHeaders(username, password)),
                List.class);

        return response.getBody();
    }

    public List<LinkedHashMap> findByModel(String model) {

        RestTemplate restTemplate = new RestTemplate();

        String url = App.getUrl() + MachineRestServiceImpl.URL_FIND_BY_MODEL + model;
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);


        ResponseEntity<List> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createJSONHeaders(username, password)),
                List.class);

        return response.getBody();

    }

    public boolean isCodeUnique(String code) {
        RestTemplate restTemplate = new RestTemplate();
        
        String url = App.getUrl() + URL_IS_CODE_UNIQUE + code;
        
        String username = App.getUsername();
        String password = App.getPassword();

        MappingJacksonHttpMessageConverter con = new MappingJacksonHttpMessageConverter();

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(con);

        restTemplate.setMessageConverters(converters);

        ResponseEntity<Boolean> response =
                restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<Object>(Utils.createHeaders(username, password)),
                Boolean.class);

        return response.getBody();
    }
}
